package chpt05.shopping.ui;

import java.util.List;

import chpt05.shopping.Commodity;
import chpt05.shopping.ShoppingCart;
import chpt05.shopping.ShoppingCart.CKResult;
import chpt05.shopping.ShoppingCart.ItemEntry;

public class ConsoleMenuImpl implements IMenu {

	@Override
	public void showShoppingCart(ShoppingCart cart) {
		printShoppingCart(cart);
	}
	private static void printShoppingCart(ShoppingCart cart) {
		System.out.println("***购物车清单***");
		System.out.printf("编号\t商品\t详情\t\t数量\t单价%n");
		ItemEntry[] itemList = cart.getItems();
		printItemList(itemList);              // 输出购物车商品条目
		printCheckOutResult(cart.checkout()); // 输出结算结果
		System.out.println("**************");
	}

	// 打印商品列表
	private static void printItemList(ItemEntry[] items) {
		for (int i = 0; i < items.length; i++) {
			ItemEntry item = items[i];
			Commodity c = item.getItem();
			System.out.printf("%d\t%-10s\t%-10s\t%-5s\t%.2f%n", 
					i + 1, c.getName(), c.getDescription(), 
					item.getQty(),c.getPrice());
		}
	}

	// 打印结算结果
	private static void printCheckOutResult(CKResult r) {
		System.out.println("商品数量：" + r.getQty());
		System.out.println("商品总价：" + r.getPrice());
	}

}
